//
//  FindPwdViewController.swift
//  DSHCMerchant
//
//  Created by 罗德良 on 2018/5/6.
//  Copyright © 2018年 袋鼠好车. All rights reserved.
//

import UIKit
import Alamofire
class FindPwdViewController: BaseViewController {

    @IBOutlet var phoneTF: UITextField!
    
    @IBOutlet var codeTF: UITextField!
    
    @IBOutlet var getCodeBtn: JKCountDownButton!
    
    @IBOutlet var newPwdTF: UITextField!
    
    @IBOutlet var newAgainPwdTF: UITextField!
    
    @IBOutlet var sumbitBtn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()


    }
    
    

    @IBAction func sumbitPress(_ sender: UIButton) {
        if phoneTF.text! == "" || newPwdTF.text! == "" || newAgainPwdTF.text! == "" || codeTF.text! == ""{
            MBProgressHUD.alertShow(in: self.view, message: "请填写完整再提交", hidenAfter: 0.8, mode: MBProgressHUDMode.text)
            
            return
        }
        if newPwdTF.text != newAgainPwdTF.text {
            MBProgressHUD.alertShow(in: self.view, message: "两次输入的密码不一致", hidenAfter: 0.8, mode: MBProgressHUDMode.text)
            return
        }
        requestFindPwd(phone: phoneTF.text!, password: newPwdTF.text!, againPassword: newAgainPwdTF.text!, code: codeTF.text!)
    }
    
    @IBAction func getCodeBtnPress(_ sender: JKCountDownButton) {
        if phoneTF.text?.characters.count != 11 {
                MBProgressHUD.alertShow(in: self.view, message: "请输入正确的手机号", hidenAfter: 0.8, mode: MBProgressHUDMode.text)

                return
        }
        getCodeBtn.start(withSecond: 60)
        getCodeBtn.didFinished { (_, _) -> String? in
            return "获取验证码"
        }
        getCodeBtn.isEnabled = false
        requestGetVerifyCode(phone: phoneTF.text!, type: "1")
    }
    
    //网络请求
    //提交找回密码
    func requestFindPwd(phone:String,password:String,againPassword:String,code:String) -> Void {
        Alamofire.request(findPasswordUrl, method:.post, parameters:["phone":phone,"password":againPassword,"password2":againPassword,"code":code])
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    var basemodel = BaseResponseOfModel<CommonModel>()
                    basemodel = BaseResponseOfModel.deserialize(from: response.value)!
                    
                    switch basemodel.status! {
                    case true:
                        let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
                        hud?.mode = MBProgressHUDMode.text
                        hud?.labelText = "提交找回密码成功"
                        hud?.show(animated: true, whileExecuting: {                        Thread.sleep(forTimeInterval: 0.8)
                        }, completionBlock: {                    self.navigationController?.popViewController(animated: true)
                        })

                    case false:
                    MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                case false:
                    print(response.result.error!)
                }
        }
    }
    
    //获取验证码
    func requestGetVerifyCode(phone:String,type:String) -> Void {
        Alamofire.request(getVerificationCodeUrl, method:.post, parameters:["phone":phone,"type":type])
            .responseString { response in
                
                switch response.result.isSuccess {
                case true:
                    var basemodel = BaseResponseOfModel<CommonModel>()
                    basemodel = BaseResponseOfModel.deserialize(from: response.value)!
                    
                    switch basemodel.status! {
                    case true:
                        print("发送验证码成功")
                    case false:
                   MBProgressHUD.alertShow(in: self.view, message:basemodel.msg, hidenAfter: 0.8, mode: MBProgressHUDMode.text)
                    }
                case false:
                    print(response.result.error!)
                }
        }
    }
    
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
